package com.xymtop.Controller;

import com.xymtop.Annotation.Controller;
import com.xymtop.Annotation.Mapping;
import com.xymtop.Annotation.Rest;
import com.xymtop.Entity.Grades;
import com.xymtop.Server.ResoultJson;
import com.xymtop.Server.Util.DBService;
import com.xymtop.Vo.GradesVo;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName : GradesController
 * @Description : 成绩相关控制器
 * @Author : 肖叶茂
 * @Date: 2022/12/14  6:13
 */
@Controller(router = "grades")
public class GradesController extends HttpServlet {
    @Rest
    @Mapping(url = "getList")
    public ResoultJson<GradesVo> getList(HttpServletRequest request, HttpServletResponse response) throws ClassNotFoundException, InstantiationException, IllegalAccessException {

        List<Object> objects = new ArrayList<>();
        String userid = request.getParameter("userid");
        if(userid == null){
            objects = DBService.Get("SELECT * FROM `javaweb`.`checkgrades`", GradesVo.class.getCanonicalName());

        }else {
         objects = DBService.Get("SELECT * FROM `javaweb`.`checkgrades` where stuid = "+userid, GradesVo.class.getCanonicalName());

        }
      if(objects.isEmpty()){
          return  new ResoultJson(204);
      }else {
          return  new ResoultJson(0,objects,"");
      }
    }




    @Rest
    @Mapping(url = "changeGrades")
    public ResoultJson<Grades> changeGrades(HttpServletRequest request,HttpServletResponse response) throws ClassNotFoundException, InstantiationException, IllegalAccessException {
        String id = request.getParameter("id");
        if(!DBService.Get("SELECT * FROM `grades` where id = "+id, Grades.class.getCanonicalName()).isEmpty()){
//            需要修改的内容
            String text = request.getParameter("text");
            String value = request.getParameter("value");

          int flag =   DBService.execSql("UPDATE `grades` SET "+ text +" = '"+value+"' WHERE `grades`.`id` = '"+id+"'; ");

          if(flag>0){
              return  new ResoultJson(200);
          }else {
              return  new ResoultJson(204);
          }
        }else {
            Grades grades = new Grades();
            grades.id = id;
            grades.exam = request.getParameter("exam");
            grades.process = request.getParameter("process");
            grades.scores =String.valueOf(Integer.valueOf(request.getParameter("exam"))+Integer.valueOf(request.getParameter("process"))/2);
            if(DBService.InsertObject(grades,"grades")){
                return  new ResoultJson(200);
            }else {
                return  new ResoultJson(204);
            }
        }
    }


    @Rest
    @Mapping(url = "add")
    public ResoultJson<String> add(HttpServletRequest request,HttpServletResponse response) throws ClassNotFoundException, IllegalAccessException {
        String id = request.getParameter("id");
        String process = request.getParameter("process");
        String exam = request.getParameter("exam");

        String detail = request.getParameter("detail");
        String material = request.getParameter("material");

        Grades grades = new Grades();
        grades.id = id;
        grades.process = process;
        grades.exam =exam;
        grades.scores = exam;
        grades.detail = detail;
        grades.material = material;

        if(DBService.InsertObject(grades,"grades")){
            return new ResoultJson<>(200);
        }else {
            return new ResoultJson<>(204);
        }
    }
}
